﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JdFormsDll
{
    internal partial class MyFormMessage : MyForm
    {

        internal string Message { get; set; }
        internal string Caption { get; set; }
        internal MessageBoxButtons Buttons { get; set; }
        internal MessageBoxIcon MsgIcon { get; set; }
        internal MessageBoxDefaultButton Button { get; set; }

        internal MyFormMessage()
        {
            this.ShowInTaskbar = false;
            InitializeComponent();
            base.Text = string.Empty;
        }

        private void MyFormMessage_Load(object sender, EventArgs e)
        {
            this.IsResizable = false;
            this.IsMoveable = true;
            this.ControlBox = false;
            this.TextCaptionAlignment = ContentAlignment.MiddleLeft;
            this.TextCaptionShadow = Shadow.Low;
            this.TextCaptionFont = new Font("Verdana", 18f, FontStyle.Bold);
            this.ShowIcon = false;
            this.SetButtons();
            if (Button == MessageBoxDefaultButton.Button1) button1.Focus();
            if (Button == MessageBoxDefaultButton.Button2) button2.Focus();
            if (Button == MessageBoxDefaultButton.Button3) button3.Focus();
            if (MsgIcon == MessageBoxIcon.Information)
            {
                this.Text = "Information";
                this.ShowIcon = true;
            }
            if (MsgIcon == MessageBoxIcon.Exclamation)
            {
                this.Text = "Exclamation";
                this.ShowIcon = true;
            }
            if (MsgIcon == MessageBoxIcon.Question)
            {
                this.Text = "Question";
                this.ShowIcon = true;
            }
            if (MsgIcon == MessageBoxIcon.Error)
            {
                this.Text = "Error";
                this.ShowIcon = true;
            }
            this.IconImage = JdHelper.MsgBoxIcon(MsgIcon);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics gp = e.Graphics;
            gp.SmoothingMode = SmoothingMode.HighQuality;
            gp.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            Rectangle rect = JdHelper.ClientArea(this.ClientRectangle, this.Padding);
            rect.Height -= 40;
            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                using (Font fnt = new Font("Verdana", 10f, FontStyle.Regular))
                    gp.DrawString(this.Message, fnt, new SolidBrush(this.IconColor()), rect, sf);
            }
        }

        private Color IconColor()
        {
            Color c = Color.Empty;
            if (this.BackColor.IsEqualTo(this.BackColorShadow, this.ColorsDifferencePercent))
            {
                if (this.BackColor.IsLightColor())
                    c = this.BackColor.Change(-.5d);
                else
                    c = this.BackColor.Change(.5d);
            }
            else
            {
                c = this.BackColorShadow;
            }
            return c;
        }

        private void SetButtons()
        {
            Rectangle rect = this.ClientRectangle;
            int Y = rect.Bottom - (int)(button1.Height * 1.5f);
            switch (Buttons)
            {
                case MessageBoxButtons.OK:
                    button1.Visible = false;
                    button3.Visible = false;
                    button2.Location = new Point(rect.Width / 2 - button2.Width / 2 + rect.Left, Y);
                    button2.Text = "OK";
                    button2.Visible = true;
                    break;
                case MessageBoxButtons.AbortRetryIgnore:
                    button1.Location = new Point(rect.Left + 20, Y);
                    button1.Text = "Abort";
                    button2.Location = new Point(rect.Width / 2 - button2.Width / 2 + rect.Left, Y);
                    button2.Text = "Retry";
                    button3.Location = new Point(rect.Right - 20 - button3.Width, Y);
                    button3.Text = "Ignore";
                    button1.Visible = true;
                    button2.Visible = true;
                    button3.Visible = true;
                    break;
                case MessageBoxButtons.YesNoCancel:
                    button1.Location = new Point(rect.Left + 20, Y);
                    button1.Text = "Yes";
                    button2.Location = new Point(rect.Width / 2 - button2.Width / 2 + rect.Left, Y);
                    button2.Text = "No";
                    button3.Location = new Point(rect.Right - 20 - button3.Width, Y);
                    button3.Text = "Cancel";
                    button1.Visible = true;
                    button2.Visible = true;
                    button3.Visible = true;
                    break;
                case MessageBoxButtons.OKCancel:
                     button1.Location = new Point(rect.Left + 20, Y);
                    button1.Text = "OK";
                    button3.Location = new Point(rect.Right - 20 - button3.Width, Y);
                    button3.Text = "Cancel";
                    button1.Visible = true;
                    button2.Visible = false;
                    button3.Visible = true;
                    break;
                case MessageBoxButtons.RetryCancel:
                    button1.Location = new Point(rect.Left + 20, Y);
                    button1.Text = "Retry";
                    button3.Location = new Point(rect.Right - 20 - button3.Width, Y);
                    button3.Text = "Cancel";
                    button1.Visible = true;
                    button2.Visible = false;
                    button3.Visible = true;
                    break;
                case MessageBoxButtons.YesNo:
                    button1.Location = new Point(rect.Left + 20, Y);
                    button1.Text = "Yes";
                    button3.Location = new Point(rect.Right - 20 - button3.Width, Y);
                    button3.Text = "No";
                    button1.Visible = true;
                    button2.Visible = false;
                    button3.Visible = true;
                    break;
                default:
                    button1.Visible = false;
                    button2.Visible = false;
                    button3.Visible = false;
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.None;
            if (this.Buttons == MessageBoxButtons.AbortRetryIgnore) this.DialogResult = DialogResult.Abort;
            if (this.Buttons == MessageBoxButtons.OKCancel) this.DialogResult = DialogResult.OK;
            if (this.Buttons == MessageBoxButtons.RetryCancel) this.DialogResult = DialogResult.Retry;
            if (this.Buttons == MessageBoxButtons.YesNo) this.DialogResult = DialogResult.Yes;
            if (this.Buttons == MessageBoxButtons.YesNoCancel) this.DialogResult = DialogResult.Yes;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.None;
            if (this.Buttons == MessageBoxButtons.OK) this.DialogResult = DialogResult.OK;
            if (this.Buttons == MessageBoxButtons.AbortRetryIgnore) this.DialogResult = DialogResult.Retry;
            if (this.Buttons == MessageBoxButtons.YesNoCancel) this.DialogResult = DialogResult.No;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.Buttons == MessageBoxButtons.AbortRetryIgnore) this.DialogResult = DialogResult.Ignore;
            if (this.Buttons == MessageBoxButtons.OKCancel) this.DialogResult = DialogResult.Cancel;
            if (this.Buttons == MessageBoxButtons.RetryCancel) this.DialogResult = DialogResult.Cancel;
            if (this.Buttons == MessageBoxButtons.YesNo) this.DialogResult = DialogResult.No;
            if (this.Buttons == MessageBoxButtons.YesNoCancel) this.DialogResult = DialogResult.Cancel;
        }
    }
}
